package com.xxl.job.executor.service.impl;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.util.XxlJobRemotingUtil;
import com.xxl.job.executor.model.JobTask;
import com.xxl.job.executor.model.XxlJobInfo;
import com.xxl.job.executor.service.XxlJobService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @date ：Created in 2024/1/9 18:50
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class XxlJobServiceImpl implements XxlJobService {

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.group}")
    private Integer group;

    private static final String ADD_TASK_URL = "/api/addJob";
    private static final String UPDATE_TASK_URL = "/api/updateJob";
    private static final String STOP_TASK_URL = "/api/stopJob";
    private static final String START_TASK_URL = "/api/startJob";
    private static final String REMOVE_TASK_URL = "/api/removeJob";


    @Override
    public Integer addJob(JobTask jobTask) {
        XxlJobInfo xxlJobInfo = getXxlJobInfo(jobTask);
        ReturnT jobId = XxlJobRemotingUtil.postBody(adminAddresses + ADD_TASK_URL, accessToken, 10000, xxlJobInfo,
                String.class);
        if (jobId.getCode() != XxlJobContext.HANDLE_CODE_SUCCESS) {
            throw new RuntimeException("xxl-job " + jobId.getMsg());
        }
        return Integer.parseInt(jobId.getContent().toString());
    }

    @Override
    public boolean updateJob(JobTask jobTask) {
        XxlJobInfo xxlJobInfo = getXxlJobInfo(jobTask);
        ReturnT jobId = XxlJobRemotingUtil.postBody(adminAddresses + UPDATE_TASK_URL, accessToken, 10000, xxlJobInfo,
                String.class);
        return Objects.equals(XxlJobContext.HANDLE_CODE_SUCCESS, jobId.getCode());
    }

    @Override
    public boolean stopJob(JobTask jobTask) {
        XxlJobInfo xxlJobInfo = getXxlJobInfo(jobTask);
        ReturnT jobId = XxlJobRemotingUtil.postBody(adminAddresses + STOP_TASK_URL, accessToken, 10000, xxlJobInfo,
                String.class);
        return Objects.equals(XxlJobContext.HANDLE_CODE_SUCCESS, jobId.getCode());
    }

    @Override
    public boolean startJob(JobTask jobTask) {
        XxlJobInfo xxlJobInfo = getXxlJobInfo(jobTask);
        ReturnT jobId = XxlJobRemotingUtil.postBody(adminAddresses + START_TASK_URL, accessToken, 10000, xxlJobInfo,
                String.class);
        return Objects.equals(XxlJobContext.HANDLE_CODE_SUCCESS, jobId.getCode());
    }

    private XxlJobInfo getXxlJobInfo(JobTask jobTask) {
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        xxlJobInfo.setId(jobTask.getId());
        xxlJobInfo.setJobGroup(group);
        xxlJobInfo.setJobDesc(jobTask.getJobDesc());
        xxlJobInfo.setScheduleConf(jobTask.getCron());
        xxlJobInfo.setAuthor("-");
        xxlJobInfo.setScheduleType("CRON");
        xxlJobInfo.setMisfireStrategy("DO_NOTHING");
        xxlJobInfo.setExecutorRouteStrategy("FIRST");
        xxlJobInfo.setExecutorHandler(jobTask.getExecutorHandler());
        xxlJobInfo.setExecutorParam(jobTask.getParam());
        xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
        xxlJobInfo.setExecutorTimeout(0);
        xxlJobInfo.setExecutorFailRetryCount(0);
        xxlJobInfo.setTriggerStatus(jobTask.getTriggerStatus());
        xxlJobInfo.setGlueType("BEAN");
        return xxlJobInfo;
    }

    @Override
    public boolean removeJob(JobTask jobTask) {
        XxlJobInfo xxlJobInfo = getXxlJobInfo(jobTask);
        ReturnT jobId = XxlJobRemotingUtil.postBody(adminAddresses + REMOVE_TASK_URL, accessToken, 10000, xxlJobInfo,
                String.class);
        return Objects.equals(XxlJobContext.HANDLE_CODE_SUCCESS, jobId.getCode());
    }
}
